/*
 * SmartCam master processor library
 * Settings reading and writing.
 */

#ifndef __SETTINGS_H_INCLUDED
#define __SETTINGS_H_INCLUDED

#include "scutable.h"

#ifdef __cplusplus
extern "C" {
#endif

#define ELTYPE_NONE        0
#define ELTYPE_DOCUMENT    1
#define ELTYPE_OPERATIONS  2
#define ELTYPE_OPERATION   3
#define ELTYPE_TARGETS     4
#define ELTYPE_TARGET      5
#define ELTYPE_ARGUMENTS   6
#define ELTYPE_ARGUMENT    7
#define ELTYPE_RESOURCES   8
#define ELTYPE_RESOURCE    9
#define ELTYPE_LINKS       10
#define ELTYPE_LINK        11
#define ELTYPE_PROPERTY    12
#define ELTYPE_ENVIRONMENT 13
#define ELTYPE_MAPPER      14

typedef struct scs_mapper_s
{
  double maxtime,      /* Favor minimum makespan */
         avgtime,      /* Favor locality */ 
         avgsqtime;    /* Favor balanced load */
  double avgenergy;    /* Favor low power */
  double avgresources; /* Favor low resource usage */
  int    type;         /* greedy or combinatorial */
  int    evaluator;    /* flow, unrelated, or dependent */
  int    graph;        /* unconnected, tig, tdg, or dtig */
  int    delay;        /* Lookahead by delayed mapping */
  int    iterations;   /* for iterative greedy mapping */
} scs_mapper_t;

typedef struct scs_target_s
{
  struct scs_target_s *derives;
  char *name;
  int id;
  char *skeletondir;
  char *address;
  char *driver;
  int routing;
  int buffer;
  double power;
  double frequency;
  int parallel;
  char *pos;

  table_t resources;  /* scs_target_resource_t, owned, name hashed */
  table_t operations; /* scs_target_operation_t, referenced, by opid */
} scs_target_t;

typedef struct scs_resource_s
{
  char *name;
  int id;
} scs_resource_t;

typedef struct scs_target_resource_s
{
  double count;
  int shareable;

  struct scs_target_s *target;
  struct scs_resource_s *resource;
} scs_target_resource_t;

typedef struct scs_operation_s
{
  struct scs_operation_s *derives;
  char *name;
  int id;
  char *skeleton;
  char *kernel;
  int line;

  table_t arguments; /* scs_operation_argument_t, owned */
  table_t targets;   /* scs_target_operation_t, referenced */
} scs_operation_t;

typedef struct scs_operation_argument_s
{
  int id;
  char *name;
  char *datatype;
  int type;
  int intent;
  int length;

  struct scs_operation_s *operation;
} scs_operation_argument_t;

typedef struct scs_target_operation_s
{
  double affinity;
  double time;
  double energy;

  table_t arguments; /* scs_target_operation_argument_t, owned */
  table_t resources; /* scs_target_operation_resource_t, owned */

  struct scs_target_s *target;
  struct scs_operation_s *operation;  
} scs_target_operation_t;

typedef struct scs_target_operation_argument_s
{
  int buffer;      /* External buffer needed (in elements) */
  int state;       /* Internal state on this input, before output is generated (elements) */
  int delay;       /* Signal delay caused on this input's stream (in elements) */
  int elementsize; /* Size of the data elements */
  int chunksize;   /* Number of elements needed for one pass */
  int timedist;    /* regular or bulk */

  struct scs_operation_argument_s *operation_argument;
  struct scs_target_operation_s *target_operation;
} scs_target_operation_argument_t;

typedef struct scs_target_operation_resource_s
{  
  double count;

  struct scs_target_resource_s *target_resource;
  struct scs_target_operation_s *target_operation;
} scs_target_operation_resource_t;

typedef struct scs_link_s
{
  char *name;
  struct scs_target_s *source, *destination;
} scs_link_t;

extern table_t scs_targets;           /* target_t, owned */
extern table_t scs_operations;        /* operation_t, owned */
extern table_t scs_target_operations; /* target_operation_t, owned */
extern table_t scs_links;             /* link_t, owned */
extern scs_mapper_t scs_mapper;

void scSetInit(void);
void scSetRead(char *name);
void scSetWrite(char *name);
void scSetIndex(void);
void scSetPrint(FILE *file);

#ifdef __cplusplus
}
#endif

#endif /* __SETTINGS_H_INCLUDED */
